package com.neusoft.neu.fy25g01.n98.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.neusoft.neu.fy25g01.n98.entity.CommonResult;
import com.neusoft.neu.fy25g01.n98.entity.Grid;
import com.neusoft.neu.fy25g01.n98.entity.ResultCode;
import com.neusoft.neu.fy25g01.n98.mapper.GridMapper;
import com.neusoft.neu.fy25g01.n98.service.GridService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.DelegatingServerHttpResponse;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GridServiceImpl implements GridService {
    @Autowired
    private GridMapper gridMapper;

    @Override
    public CommonResult<Integer> addGrid(Grid grid) {
        grid.setGid(null);
        int i = gridMapper.insert(grid);
        if (i > 0) {
            return CommonResult.success(grid.getGid());
        } else
            return CommonResult.failed(ResultCode.FAIL);

    }

    @Override
    public CommonResult<Integer> delGrid(Integer gid) {
//        LambdaQueryWrapper<Grid> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(Grid::getGid, gid);
//        final var l = gridMapper.selectCount(wrapper);
//        if (l == 0) return CommonResult.failed(ResultCode.DELETE_FAIL_FOR_NOTEXISTS);
//        int i = gridMapper.deleteById(gid);
//        if (i > 0)
//            return CommonResult.success();
//        else
//            return CommonResult.failed(ResultCode.FAIL);
        Grid grid = new Grid();
        grid.setGid(gid);
        grid.setStatus(-1);

        int i = gridMapper.updateById(grid);
        if (i > 0)
            return CommonResult.success();
        else
            return CommonResult.failed(ResultCode.FAIL);

    }

    @Override
    public CommonResult<List<Grid>> findAll() {

        LambdaQueryWrapper<Grid> wrapper = new LambdaQueryWrapper<>();
        wrapper.ne(Grid::getStatus, -1);

        List<Grid> list = gridMapper.selectList(wrapper);
        if (list == null || list.size() == 0)
            return CommonResult.failed(ResultCode.NOT_FOUND);
        return CommonResult.success(list);
    }

    @Override
    public CommonResult<Grid> findById(Integer gid) {
        LambdaQueryWrapper<Grid> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Grid::getGid, gid);
        Grid grid = gridMapper.selectOne(wrapper);
        if (grid == null)
            return CommonResult.failed(ResultCode.NOT_FOUND);
        return CommonResult.success(grid);
    }

    @Override
    public CommonResult<Integer> modGrid(Grid grid) {
        if (grid == null || grid.getGid() == null)
            return CommonResult.failed(ResultCode.DATA_NOT_ENOUGH);
        LambdaQueryWrapper<Grid> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Grid::getGid, grid.getGid());
        final var l = gridMapper.selectCount(wrapper);
        if (l == 0) return CommonResult.failed(ResultCode.UPDATE_FAIL_FOR_NOTEXISTS);
        int i = gridMapper.updateById(grid);
        if (i > 0)
            return CommonResult.success();
        else
            return CommonResult.failed(ResultCode.FAIL);

    }
}
